package com.caterina.components;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.caterina.activities.LogoutActivity;
import com.caterina.activities.SpringBoardActivity;
import com.caterina.activities.UserInfoActivity;
import com.caterina.activityManager.ActivityManager;
import com.caterina.resources.CPaths;
import com.caterina.resources.CWords;

public class CBar extends JPanel {
	private static final long serialVersionUID = 1L;
	public CBar(String titleText,ActivityManager manager,Container parent){
		super(new GridLayout(1,3));
		this.manager=manager;
		this.parent=parent;
		this.addLeft();
		this.addTitle(titleText);
		this.addRight("");
		this.setPreferredSize(new Dimension(0, 31));
		this.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.gray));
		this.setBackground(new Color(216,216,216));
	}
	private void addSpringButton(){
		this.spring=new JLabel();
		spring.setIcon(new ImageIcon(CPaths.ConvertUrl("graphics\\toSpring.png")));
		leftPanel.add(spring);
		spring.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {CBar.this.manager.switchActivity(new SpringBoardActivity(parent));}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
	}
	private void addLogoutButton(){
		this.logout=new JLabel();
		logout.setIcon(new ImageIcon(CPaths.ConvertUrl("graphics\\logout.png")));
		leftPanel.add(logout);
		logout.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {
				if(JOptionPane.showConfirmDialog(
						CBar.this.manager.getCurrentActivity(), 
						CWords.getWord("LOGOUT_MESSAGE"), 
						"Logout",JOptionPane.YES_NO_OPTION
						)==0)
				CBar.this.manager.switchActivity(new LogoutActivity(parent));
				}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
	}
	private void addLogo(){
		this.logo=new JLabel();
		logo.setIcon(new ImageIcon(CPaths.ConvertUrl("graphics\\logoBar.png")));
		leftPanel.add(logo);
	}
	private void addLeft(){
		this.leftPanel=new JPanel();
		this.leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.X_AXIS));
		this.leftPanel.setOpaque(false);
		this.addLogo();
		this.addLogoutButton();
		this.addSpringButton();
		this.add(leftPanel);
	}
	private void addTitle(String titleText){
		title=new JPanel();
		title.setBorder(BorderFactory.createMatteBorder(6, 10, 0, 0, new Color(216,216,216)));
		title.setOpaque(false);
		title.setLayout(new BoxLayout(title, BoxLayout.Y_AXIS));
		lbltitle=new JLabel(titleText);
		lbltitle.setAlignmentX(CENTER_ALIGNMENT);
		lbltitle.setAlignmentY(CENTER_ALIGNMENT);
		lbltitle.setFont(new Font("Tahoma",Font.PLAIN,15));
		title.add(lbltitle);
		this.add(title);
	}
	private void addRight(String text){
		this.right=new JPanel();
		this.right.setLayout(new BoxLayout(right,BoxLayout.X_AXIS));
		right.setBackground(new Color(216,216,216));
		right.setBorder(BorderFactory.createMatteBorder(6, 0, 0, 10, new Color(216,216,216)));
		right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
		lblRight=new JLabel(text);
		lblRight.setAlignmentX(RIGHT_ALIGNMENT);
		lblRight.setAlignmentY(CENTER_ALIGNMENT);
		lblRight.setFont(new Font("Tahoma",Font.PLAIN,15));
		right.add(lblRight);
		this.right.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}		
			@Override
			public void mouseExited(MouseEvent e) {}		
			@Override
			public void mouseEntered(MouseEvent e) {}		
			@Override
			public void mouseClicked(MouseEvent e) {
				CBar.this.manager.switchActivity(new UserInfoActivity(parent));
			}
		});
		this.add(right);
	}
	public void setTitle(String title){
		this.lbltitle.setText(title);
	}
	public void setSbButtonVisible(boolean isVisible){
		this.spring.setVisible(isVisible);
	}
	public void setLogoutButtonVisible(boolean isVisible){
		this.logout.setVisible(isVisible);
	}
	public void setRightText(String rightText){
		this.lblRight.setText(rightText);
	}
	public void setActivityManager(ActivityManager activityManager) {
		this.manager=activityManager;
	}
	private Container parent;
	private ActivityManager manager;
	private JPanel leftPanel;
	private JLabel logo;
	private JLabel logout;
	private JLabel spring;
	private JPanel title;
	private JPanel right;
	private JLabel lbltitle;
	private JLabel lblRight;
}
